<?php
use Illuminate\Database\Capsule\Manager as Capsule;

class Bootstrap extends Yaf_Bootstrap_Abstract
{
    protected $config;

    public function _initConfig(Yaf_Dispatcher $dispatcher)
    {
        $this->config = Yaf_Application::app()->getConfig();
        Yaf_Registry::set('config', $this->config);
    }

    public function _initAutoload(Yaf_Dispatcher $dispatcher)
    {
        Yaf_Loader::import(APP_PATH . '/conf/function.php');
        Yaf_Loader::import(APP_PATH . "/vendor/autoload.php");
    }

    public function _initError(Yaf_Dispatcher $dispatcher)
    {
        if ($this->config->application->debug) {
            define('DEBUG_MODE', true);
            ini_set('display_errors', 'On');
        } else {
            define('DEBUG_MODE', false);
            ini_set('display_errors', 'Off');
        }
    }

    public function _initCrossdomain(Yaf_Dispatcher $dispatcher)
    {
        $origin = $_SERVER['HTTP_ORIGIN']??'';
        $allow_origin = array(
            'http://localhost:8080',
            'http://shop.zy52.cn',
            'http://xyc.xyckj178.top',
            'http://jinmu.xyckj178.top'
        );
        header('Access-Control-Allow-Origin: *');
        if(in_array($origin, $allow_origin)){
            header('Access-Control-Allow-Origin:'.$origin);
        }
        header("Access-Control-Allow-Methods: PUT,POST,GET,OPTIONS,DELETE");
        header("Access-Control-Allow-Headers: Authorization, platform, Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie, Token");
        header('Access-Control-Max-Age:86400000');
        if($dispatcher->getRequest()->getMethod() === 'OPTIONS'){exit;}
    }

    public function _initPlugin(Yaf_Dispatcher $dispatcher)
    {
        //cookie涉及HTTP请求，命令行下应禁用
        if ($dispatcher->getRequest()->getMethod() != 'CLI') {
            $antizy = new antizyPlugin();
            $dispatcher->registerPlugin($antizy);

            if ($this->config->application->benchmark) {
                $benchmark = new benchmarkPlugin();
                $dispatcher->registerPlugin($benchmark);
            }
        }
        //事件监听
        $hook = new hookPlugin();
        $dispatcher->registerPlugin($hook);
    }

    public function _initRoute(Yaf_Dispatcher $dispatcher)
    {
        $routes = $this->config->routes;
        if (!empty($routes)) {
            $router = $dispatcher->getRouter();
            $router->addConfig($routes);
        }
    }

    public function _initRedis()
    {
        if ($this->config->cache->redis->enable) {
            define('CACHE_ENABLE', true);
            define('CACHE_KEY_PREFIX', $this->config->cache->redis->prefix);
        } else {
            define('CACHE_ENABLE', false);
        }
    }

    public function _initDatabase()
    {
        $capsule = new Capsule;
        // 创建默认链接
        $capsule->addConnection(Yaf_Application::app()->getConfig()->database->toArray());
        // biz业务链接
        // $capsule->addConnection(Yaf_Application::app()->getConfig()->biz->toArray(), 'biz');
        // 设置全局静态可访问
        $capsule->setAsGlobal();
        // 启动Eloquent
        $capsule->bootEloquent();
    }

    public function _initQueue()
    {
        //application.queue = TRUE
        //queue为false时，自动切为同步操作
        if ($this->config->application->queue) {
            define('QUEUE_ENABLE', true);
        } else {
            define('QUEUE_ENABLE', false);
        }
    }

}
